package com.tanhua.manage.listener;

import com.tanhua.commons.template.HuaWeiUGCTemplate;
import com.tanhua.domain.mongo.Publish;
import com.tanhua.dubbo.api.mongo.PublishApi;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 消息监听器，监听ttanhua-publish主题中的消息
 */
@Component
@RocketMQMessageListener(topic = "tanhua-publish",consumerGroup = "tanhuan-publish-consumer")
public class PublishMessageListener implements RocketMQListener<String> {

    @Reference
    private PublishApi publishApi;
    @Autowired
    private HuaWeiUGCTemplate huaWeiUGCTemplate;

    @Override
    public void onMessage(String publishId) {
        //1. 根据动态id查询
        Publish publish = publishApi.findById(publishId);
        if (publish != null && publish.getState() == 0) {
            //2. 调用华为云内容审核
            boolean contentCheck =
                    huaWeiUGCTemplate.textContentCheck(publish.getTextContent());
            if (contentCheck) {
                boolean imageCheck =
                        huaWeiUGCTemplate.imageContentCheck(publish.getMedias().toArray(new String[]{}));
                Integer state = 2; // 默认审核不通过
                if (imageCheck) {
                    state = 1;// 审核通过
                }
                //3. 审核通过，修改发布状态state=1
                publishApi.updateState(publishId,state);
            }
        }
    }
}
